package com.eqdd.yiqidian.ui.companyspace;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewRefreshCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.AdvancedEventBean;
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * Created by jhj on 18-6-9.
 */

public class AdvancedEventListActivity extends BaseActivity {

    RecyclerViewRefreshCustom dataBinding;
    private int pageNum;
    private MVCCoolHelper<List<AdvancedEventBean>> mvcHelper;
    private SlimAdapterEx<AdvancedEventBean> slimAdapterEx;
    private int type = 0;
    private int companyId = 0;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_refresh);
        initTopTitleBar(View.VISIBLE, "先进事迹");
        type = getIntent().getIntExtra(Config.TYPE, 0);
       /* if (type == 0) {
            initTopRightText("新增", view -> {
                Bundle bundle = new Bundle();
                startActivityForResult(AdvancedEventAddActivity.class, bundle, Config.ADD);
            });
        }*/

    }

    @SuppressWarnings("unchecked")
    @Override
    public void initData() {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.addItemDecoration(new DividerLineItemDecoration(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_154, new SlimInjector<AdvancedEventBean>() {
            @Override
            public void onInject(AdvancedEventBean data, IViewInjector injector) {
                injector
                        .text(R.id.tv_title, data.getTitle())
                        .text(R.id.tv_time, data.getCreateTime());
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        if (type == 0) {
            ItemClickSupport.addTo(dataBinding.recyclerView).setOnItemClickListener((recyclerView, position, v) -> {
                Bundle bundle = new Bundle();
                bundle.putInt(Config.ID, slimAdapterEx.getDataItem(position).getId());
                startActivityForResult(AdvancedEventInfoActivity.class, bundle, Config.UPDATE);
            });
            companyId = user.getCompanyId();
        } else {
            companyId = getIntent().getIntExtra(Config.COMPANY_ID, 0);
            if (companyId == 0) {
                ToastUtil.showShort("没有数据");
                finish();
            }
        }


        ModelRx2DataSource<AdvancedEventBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                pageNum = page == 1 ? 0 : pageNum;
                return OkGo.<HttpResult<List<AdvancedEventBean>>>post(HttpConfig.BASE_URL + HttpConfig.ADVANCED_EVENT_LIST)
                        .params("companyId", companyId)
                        .params("page", pageNum)
                        .converter(new JsonConverter<HttpResult<List<AdvancedEventBean>>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        }).adapt(new FlowableBody<>())
                        .flatMap(result -> {
                            if (page == 1) {
                                mvcHelper.setAutoLoadMore(true);
                            }
                            pageNum = result.getPage();
                            return Flowable.just(result.getItems() == null ? new ArrayList() : result.getItems());
                        });
            }
        }, 10);
        mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.refresh();


    }

    @Override
    public void setView() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.ADD && resultCode == RESULT_OK) {
            mvcHelper.refresh();
        }
        if (requestCode == Config.UPDATE && resultCode == RESULT_OK) {
            mvcHelper.refresh();
        }
    }
}
